package com.li.commons.util;
import java.util.List;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;

public class DbDocUtils {

	public static void main(String[] arg) throws FileNotFoundException, DocumentException {
		String url = "jdbc:mysql://123.57.68.212:3306/information_schema?useUnicode=true";
		String user = "btc_user";
		String password = "ikongjian*()";
		String dbName = "www";
		List<String> tableNames = getTables(url, user, password, dbName);
		//创建文档
		Document document = createDocument("/Users/huqilong/Desktop/线上运营数据库文档.doc");
		document.open();
		//标题字体风格
  		Font titleFont = new Font(Font.NORMAL, 25, Font.BOLD, new Color(0, 50, 0));
  		//正文字体风格
  		Font contextFont = new Font(Font.NORMAL, 14, Font.NORMAL, new Color(0, 0, 0));
  		Paragraph title = new Paragraph("线上运营数据库文档");
  		//设置标题格式对齐方式
  		title.setAlignment(Element.ALIGN_CENTER);
  		title.setFont(titleFont);
  		document.add(title);
  		//正文
  		Paragraph context = new Paragraph("测试导出word");
  		context.setAlignment(Element.ALIGN_LEFT);
  		context.setFont(contextFont);
  		//段间距
  		context.setSpacingBefore(3);
  		//设置第一行空的列数
  		context.setFirstLineIndent(20);
  		document.add(context);
		for (String tableName : tableNames) {
			Table table = createTable(tableName);
			List<String[]> fieldsList = getTableField(url, user, password,dbName,tableName);
			for (String[] fields : fieldsList) {
				for(int i=0;i<7;i++){
					table.addCell(new Cell(fields[i]));
				}
			}
			document.add(table);
			Paragraph blank = new Paragraph(" ");
			blank.setAlignment(Element.ALIGN_LEFT);
			blank.setFont(contextFont);
	  		//段间距
			blank.setSpacingBefore(3);
		}
		document.close();
	}
	
	private static Document createDocument(String target) throws FileNotFoundException{
		Document document = new Document(PageSize.A4);
  		//建立一个书写器，与document对象关联
  		RtfWriter2.getInstance(document, new FileOutputStream(target));
  		return document;
	}
	
	private static Table createTable(String tableName) throws DocumentException {
		Table table = new Table(7);
  		int width[] = {25,25,25,25,25,25,25};//设置每列宽度比例
  		table.setWidths(width);
  		table.setWidth(90);//占页面宽度比例
  		table.setAlignment(Element.ALIGN_CENTER);//居中
  		table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
  		table.setAutoFillEmptyCells(true);//自动填满
  		table.setBorderWidth(1);//边框宽度
  		//设置表头
  		Cell headerCell = new Cell(tableName);
  		headerCell.setHeader(true);
  		headerCell.setColspan(7);
  		headerCell.setBackgroundColor(Color.blue);
  		table.addCell(headerCell);
  		table.endHeaders();
  		
  		//设置表头
  		Cell cell1 = new Cell("列名");
  		cell1.setBackgroundColor(Color.yellow);
  		table.addCell(cell1);
  		
  		Cell cell2 = new Cell("类型");
  		cell2.setBackgroundColor(Color.yellow);
  		table.addCell(cell2);
  		
  		Cell cell3 = new Cell("主键");
  		cell3.setBackgroundColor(Color.yellow);
  		table.addCell(cell3);
  		
  		Cell cell4 = new Cell("是否为空");
  		cell4.setBackgroundColor(Color.yellow);
  		table.addCell(cell4);
  		
  		
  		Cell cell5 = new Cell("默认值");
  		cell5.setBackgroundColor(Color.yellow);
  		table.addCell(cell5);
  		
  		Cell cell6 = new Cell("自增");
  		cell6.setBackgroundColor(Color.yellow);
  		table.addCell(cell6);
  		
  		Cell cell7 = new Cell("是否为空");
  		cell7.setBackgroundColor(Color.yellow);
  		table.addCell(cell7);
  		return table;
	}

	/*
	 * 获得所有表名
	 */
	private static List<String> getTables(String url, String user,String password, String dbName) {
		List<String> tables = MataDataTool.loadTables(url, user, password,dbName);
		return tables;
	}

	private static List<String[]> getTableField(String url, String user, String password, String dbName, String tableName) {
		return MataDataTool.loadTableFields(url, user, password, dbName, tableName);
	}

	
}
